package com.mark.web.sys.controller.backend;

import com.github.pagehelper.PageInfo;
import com.mark.common.ServerResponse;
import com.mark.web.sys.entity.from.RoleFrom;
import com.mark.web.sys.entity.from.RoleUpdateFrom;
import com.mark.web.sys.entity.po.Role;
import com.mark.web.sys.entity.vo.DeptVo;
import com.mark.web.sys.entity.vo.RoleVo;
import com.mark.web.sys.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Description: 系统角色控制器
 * @Author: Mark
 * @CreateDate: 2019/1/13 18:51
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Api(tags = "系统-后台-角色模块")
@RestController
@RequestMapping("/sys/role/")
public class RoleController {
    @Autowired
    private IRoleService iRoleService;

    @ApiOperation(value = "添加角色")
    @PostMapping("add.do")
    ServerResponse<String> addRole(@Valid RoleFrom roleFrom, BindingResult result) {
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleFrom,role);
        return iRoleService.addRole(role);
    }

    @ApiOperation(value = "编辑角色")
    @PostMapping("edit.do")
    ServerResponse<String> editRole(@Valid RoleUpdateFrom roleUpdateFrom, BindingResult result) {
        if(result.hasErrors()){
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleUpdateFrom,role);
        return iRoleService.editRole(role);
    }

    @ApiOperation(value = "获取角色信息")
    @ApiImplicitParam(name = "id",value = "角色ID",defaultValue = "1")
    @PostMapping(value = "getRole.do")
    public ServerResponse<RoleVo> getRole(Integer id){
        if(id == null){
            return ServerResponse.createByIllegalArgument("角色ID不能为空！");
        }
        return iRoleService.getRole(id);
    }

    @ApiOperation(value = "获取所有的角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前的页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",defaultValue = "4")
    })
    @PostMapping(value = "getRoleList.do")
    public ServerResponse<PageInfo<RoleVo>> getRoleList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                        @RequestParam(value = "pageSize",defaultValue = "4") int pageSize){
        return iRoleService.getRoleList(pageNum,pageSize);
    }

}
